﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Xml.Linq;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Renren.Models;
using Renren.Services;
using Renren.Xna;
using Renren.ServiceProviders;

namespace Renren.Helpers
{
    public static class Extensions
    {
        public static void Draw(this SpriteBatch sb, Photo photo)
        {
            if (null != photo.Texture)
            {
                sb.Draw(
                photo.Texture,
                photo.Position,
                null,
                Color.White,
                0,
                photo.Center,
                photo.Scale,
                SpriteEffects.None,
                0);
            }
        }

        public static IEnumerable<XElement> Elements(this XElement element, string parent, string child)
        {
            IEnumerable<XElement> children;
            var parentNode = element.Child(parent);
            if (parentNode != null)
            {
                if (child == "*")
                {
                    children = element.Child(parent).Elements();
                }
                else
                {
                    children = element.Child(parent).Elements(element.GetDefaultNamespace() + child);
                } 
            }
            else
            {
                children = new List<XElement>();
            }
            return children;
        }

        public static XElement Child(this XElement element, string name)
        {
            if (element != null)
            {
                return element.Element(element.GetDefaultNamespace() + name);
            }
            return null;
        }

        public static string GetValue(this XElement element, string parent, string child)
        {
            return GetValue<string>(element, parent, child);
        }

        public static string GetValue(this XElement element, string name)
        {
            return GetValue<string>(element, name);
        }

        public static T GetValue<T>(this XElement element, string parent, string child)
        {
            return GetValue<T>(element.Child(parent), child);
        }

        public static string GetValue(this XElement element, string parent, string child, string grandchild)
        {
            return GetValue<string>(element, parent, child, grandchild);
        }

        public static T GetValue<T>(this XElement element, string parent, string child, string grandchild)
        {
            return GetValue<T>(element.Child(parent), child, grandchild);
        }

        public static T GetValue<T>(this XElement element, string name)
        {
            var child = element.Child(name);
            if (child != null)
            {
                if (typeof(T) == typeof(int) || typeof(T) == typeof(PrivacyType) || typeof(T) == typeof(FeedType))
                {
                    return (T)(object)int.Parse(child.Value);
                }
                else if (typeof(T) == typeof(int))
                {
                    return (T)(object)int.Parse(child.Value);
                }
                else if (typeof(T) == typeof(long))
                {
                    return (T)(object)long.Parse(child.Value);
                }
                else if (typeof(T) == typeof(string))
                {
                    return (T)(object)child.Value;
                }
                else if (typeof(T) == typeof(DateTime))
                {
                    var utcOffset = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);
                    var chinaTime = DateTime.Parse(element.GetValue<string>(name));
                    var utcTime = chinaTime + new TimeSpan(-8, 0, 0);
                    return (T)(object)(utcTime + TimeZoneInfo.Local.GetUtcOffset(DateTime.Now));
                }
            }
            return default(T);
        }

        public static ServiceProvider GetServiceProvider(this ServiceProviderType type)
        {
            ServiceProvider serviceProvider = null;
            switch (type)
            {
                case ServiceProviderType.Google:
                    serviceProvider = GoogleServiceProvider.Instance;
                    break;
                case ServiceProviderType.Renren:
                    serviceProvider = RenrenServiceProvider.Instance;
                    break;
            }
            return serviceProvider;
        }

        public static string GetString(this PlaceType type)
        {
            string str = null;
            switch (type)
            {
                case PlaceType.accounting:
                    str = "会计";
                    break;
                case PlaceType.airport:
                    str = "机场";
                    break;
                case PlaceType.amusement_park:
                    str = "游乐场";
                    break;
                case PlaceType.aquarium:
                    str = "水族馆";
                    break;
                case PlaceType.art_gallery:
                    str = "艺术馆";
                    break;
                case PlaceType.atm:
                    str = "ATM";
                    break;
                case PlaceType.bakery:
                    str = "面包店";
                    break;
                case PlaceType.bank:
                    str = "银行";
                    break;
                case PlaceType.bar:
                    str = "酒吧";
                    break;
                case PlaceType.beauty_salon:
                    str = "发廊";
                    break;
                case PlaceType.bicycle_store:
                    str = "自行车店";
                    break;
                case PlaceType.book_store:
                    str = "书店";
                    break;
                case PlaceType.bowling_alley:
                    str = "保龄球馆";
                    break;
                case PlaceType.bus_station:
                    str = "车站";
                    break;
                case PlaceType.cafe:
                    str = "咖啡店";
                    break;
                case PlaceType.campground:
                    str = "营地";
                    break;
                case PlaceType.car_dealer:
                    str = "汽车经销商";
                    break;
                case PlaceType.car_rental:
                    str = "租车";
                    break;
                case PlaceType.car_repair:
                    str = "修车";
                    break;
                case PlaceType.car_wash:
                    str = "洗车";
                    break;
                case PlaceType.casino:
                    str = "赌场";
                    break;
                case PlaceType.cemetery:
                    str = "墓地";
                    break;
                case PlaceType.church:
                    str = "教堂";
                    break;
                case PlaceType.city_hall:
                    str = "市政厅";
                    break;
                case PlaceType.clothing_store:
                    str = "服装店";
                    break;
                case PlaceType.convenience_store:
                    str = "便利店";
                    break;
                case PlaceType.courthouse:
                    str = "法院";
                    break;
                case PlaceType.dentist:
                    str = "牙医";
                    break;
                case PlaceType.department_store:
                    str = "百货商店";
                    break;
                case PlaceType.doctor:
                    str = "医生";
                    break;
                case PlaceType.electrician:
                    str = "电工";
                    break;
                case PlaceType.electronics_store:
                    str = "电子商店";
                    break;
                case PlaceType.embassy:
                    str = "使馆";
                    break;
                case PlaceType.establishment:
                    str = "会馆";
                    break;
                case PlaceType.finance:
                    str = "金融";
                    break;
                case PlaceType.fire_station:
                    str = "消防站";
                    break;
                case PlaceType.florist:
                    str = "花商";
                    break;
                case PlaceType.food:
                    str = "食物";
                    break;
                case PlaceType.funeral_home:
                    str = "殡仪馆";
                    break;
                case PlaceType.furniture_store:
                    str = "家具店";
                    break;
                case PlaceType.gas_station:
                    str = "加油站";
                    break;
                case PlaceType.general_contractor:
                    str = "承包商";
                    break;
                case PlaceType.geocode:
                    str = "地理编码";
                    break;
                case PlaceType.grocery_or_supermarket:
                    str = "超市";
                    break;
                case PlaceType.gym:
                    str = "健身房";
                    break;
                case PlaceType.hair_care:
                    str = "护发";
                    break;
                case PlaceType.hardware_store:
                    str = "五金店";
                    break;
                case PlaceType.health:
                    str = "健康";
                    break;
                case PlaceType.hindu_temple:
                    str = "印度教寺庙";
                    break;
                case PlaceType.home_goods_store:
                    str = "家居用品店";
                    break;
                case PlaceType.hospital:
                    str = "医院";
                    break;
                case PlaceType.insurance_agency:
                    str = "保险代理";
                    break;
                case PlaceType.jewelry_store:
                    str = "珠宝店";
                    break;
                case PlaceType.laundry:
                    str = "洗衣房";
                    break;
                case PlaceType.lawyer:
                    str = "律师";
                    break;
                case PlaceType.library:
                    str = "图书馆";
                    break;
                case PlaceType.liquor_store:
                    str = "酒品店";
                    break;
                case PlaceType.local_government_office:
                    str = "地方政府";
                    break;
                case PlaceType.locksmith:
                    str = "锁匠";
                    break;
                case PlaceType.lodging:
                    str = "住宿";
                    break;
                case PlaceType.meal_delivery:
                    str = "送餐";
                    break;
                case PlaceType.meal_takeaway:
                    str = "外卖";
                    break;
                case PlaceType.mosque:
                    str = "清真寺";
                    break;
                case PlaceType.movie_rental:
                    str = "电影租赁";
                    break;
                case PlaceType.movie_theater:
                    str = "电影院";
                    break;
                case PlaceType.moving_company:
                    str = "搬家公司";
                    break;
                case PlaceType.museum:
                    str = "博物馆";
                    break;
                case PlaceType.night_club:
                    str = "夜店";
                    break;
                case PlaceType.painter:
                    str = "漆匠";
                    break;
                case PlaceType.park:
                    str = "公园";
                    break;
                case PlaceType.parking:
                    str = "停车";
                    break;
                case PlaceType.pet_store:
                    str = "宠物店";
                    break;
                case PlaceType.pharmacy:
                    str = "药房";
                    break;
                case PlaceType.physiotherapist:
                    str = "物理治疗师";
                    break;
                case PlaceType.place_of_worship:
                    str = "圣地";
                    break;
                case PlaceType.plumber:
                    str = "水管工";
                    break;
                case PlaceType.police:
                    str = "警察局";
                    break;
                case PlaceType.post_office:
                    str = "邮局";
                    break;
                case PlaceType.real_estate_agency:
                    str = "地产代理";
                    break;
                case PlaceType.restaurant:
                    str = "餐馆";
                    break;
                case PlaceType.roofing_contractor:
                    str = "屋顶承包商";
                    break;
                case PlaceType.rv_park:
                    str = "rv公园";
                    break;
                case PlaceType.school:
                    str = "rv学校";
                    break;
                case PlaceType.shoe_store:
                    str = "鞋店";
                    break;
                case PlaceType.shopping_mall:
                    str = "购物中心";
                    break;
                case PlaceType.spa:
                    str = "喷泉";
                    break;
                case PlaceType.stadium:
                    str = "体育馆";
                    break;
                case PlaceType.storage:
                    str = "储藏室";
                    break;
                case PlaceType.store:
                    str = "商店";
                    break;
                case PlaceType.subway_station:
                    str = "地铁站";
                    break;
                case PlaceType.synagogue:
                    str = "犹太教堂";
                    break;
                case PlaceType.taxi_stand:
                    str = "出租车站";
                    break;
                case PlaceType.train_station:
                    str = "火车站";
                    break;
                case PlaceType.travel_agency:
                    str = "旅行社";
                    break;
                case PlaceType.university:
                    str = "大学";
                    break;
                case PlaceType.veterinary_care:
                    str = "兽医";
                    break;
                case PlaceType.zoo:
                    str = "动物园";
                    break;
                case PlaceType.administrative_area_level_1:
                    str = "1级行政区";
                    break;
                case PlaceType.administrative_area_level_2:
                    str = "2级行政区";
                    break;
                case PlaceType.administrative_area_level_3:
                    str = "3级行政区";
                    break;
                case PlaceType.colloquial_area:
                    str = "colloquial area";
                    break;
                case PlaceType.country:
                    str = "乡村";
                    break;
                case PlaceType.floor:
                    str = "floor";
                    break;
                case PlaceType.intersection:
                    str = "路口";
                    break;
                case PlaceType.locality:
                    str = "locality";
                    break;
                case PlaceType.natural_feature:
                    str = "自然景观";
                    break;
                case PlaceType.neighborhood:
                    str = "邻居";
                    break;
                case PlaceType.political:
                    str = "政治";
                    break;
                case PlaceType.point_of_interest:
                    str = "景点";
                    break;
                case PlaceType.post_box:
                    str = "邮箱";
                    break;
                case PlaceType.postal_code:
                    str = "邮编";
                    break;
                case PlaceType.postal_code_prefix:
                    str = "postal code prefix";
                    break;
                case PlaceType.postal_town:
                    str = "邮政镇";
                    break;
                case PlaceType.premise:
                    str = "premise";
                    break;
                case PlaceType.room:
                    str = "屋子";
                    break;
                case PlaceType.route:
                    str = "路线";
                    break;
                case PlaceType.street_address:
                    str = "街道";
                    break;
                case PlaceType.street_number:
                    str = "路牌";
                    break;
                case PlaceType.sublocality:
                    str = "sublocality";
                    break;
                case PlaceType.sublocality_level_4:
                    str = "sublocality level 4";
                    break;
                case PlaceType.sublocality_level_5:
                    str = "sublocality level 5";
                    break;
                case PlaceType.sublocality_level_3:
                    str = "sublocality level 3";
                    break;
                case PlaceType.sublocality_level_2:
                    str = "sublocality level 4";
                    break;
                case PlaceType.sublocality_level_1:
                    str = "sublocality level 1";
                    break;
                case PlaceType.subpremise:
                    str = "subpremise";
                    break;
                case PlaceType.transit_station:
                    str = "中转站";
                    break;
            }
            return str;
        }

        public static string GetString(this FeedType type)
        {
            string str = null;
            switch (type)
            {
                case FeedType.UpdateStatus:
                    str = "更新状态";
                    break;
                case FeedType.PostBlog:
                    str = "发表日志";
                    break;
                case FeedType.ShareBlog:
                    str = "分享日志";
                    break;
                case FeedType.UploadPhoto:
                    str = "上传照片";
                    break;
                case FeedType.SharePhoto:
                    str = "分享照片";
                    break;
                case FeedType.ShareAlbum:
                    str = "发表相册";
                    break;
                case FeedType.ChangeProfilePhoto:
                    str = "更新头像";
                    break;
                case FeedType.BecomeFriends:
                    str = "成为好友";
                    break;
                case FeedType.ShareVideo:
                    str = "分享视频";
                    break;
                case FeedType.ShareLink:
                    str = "分享链接";
                    break;
                case FeedType.ShareMusic:
                    str = "发表音乐";
                    break;
                default:
                    break;
            }
            return str;
        }

        public static string Trim(this string str, int length)
        {
            if (str == null || length >= str.Length)
            {
                return str;
            }
            else
            {
                return str.Substring(0, length) + "...";
            }
        }

        public static string GetFriendlyTime(this DateTime time)
        {
            string friendlyTIme = "未知时间";
            var timeSpan = DateTime.Now - time;
            if (timeSpan.Days > 0)
            {
                friendlyTIme = timeSpan.Days + "天前";
            }
            else if (timeSpan.Hours > 0)
            {
                friendlyTIme = timeSpan.Hours + "小时前";
            }
            else if (timeSpan.Minutes > 0)
            {
                friendlyTIme = timeSpan.Minutes + "分钟前";
            }
            else if (timeSpan.Seconds > 0)
            {
                friendlyTIme = timeSpan.Minutes + "秒前";
            }
            else if (timeSpan.Seconds <= 0)
            {
                friendlyTIme = "刚刚更新";
            }
            return friendlyTIme;
        }

        //public static void AddProgressIndicator(this PhoneApplicationPage page)
        //{
        //    page.Loaded += (sender, args) =>
        //    {
        //        ProgressIndicator pi = SystemTray.ProgressIndicator;
        //        if (pi == null)
        //        {
        //            pi = new ProgressIndicator();
        //            SystemTray.SetProgressIndicator(page, pi);
        //            pi.IsIndeterminate = true;
        //            pi.IsVisible = true;
        //        }
        //    };
        //}

        //public static ServiceType GetServiceType(this ActionType action)
        //{
        //    ServiceType type = default(ServiceType);
        //    switch (action.ToString().Split('_')[0])
        //    {
        //        case "Blog":
        //            type = ServiceType.Blog;
        //            break;
        //        case "Feed":
        //            type = ServiceType.Feed;
        //            break;
        //        case "Friends":
        //            type = ServiceType.Friends;
        //            break;
        //        case "Photos":
        //            type = ServiceType.Photos;
        //            break;
        //        case "Status":
        //            type = ServiceType.Status;
        //            break;
        //        case "Users":
        //            type = ServiceType.Users;
        //            break;
        //    }
        //    return type;
        //}

        public static void NavigateTo(this UserControl control, string page, object arg)
        {
            PhoneApplicationService.Current.State[page] = arg;
            (App.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/" + page + ".xaml", UriKind.Relative));
        }
    }
}
